<?php




class Mysql{
	private $db_host;
	private $db_user;
	private $db_pwd;
	private $db_name;
	private $db_charset;	
	
	private $conn;


	public function __construct(){
		$this->db_host=DB_HOST;
		$this->db_user=DB_USER;
		$this->db_pwd=DB_PASS;
		$this->db_name=DB_NAME;
		$this->db_charset=DB_CHSET;
		$this->conn=mysql_connect($this->db_host,$this->db_user,$this->db_pwd);
		if(!$this->conn){
			 throw new Exception("连接错误，请检查数据库配置信息");		
		}
		mysql_select_db($this->db_name,$this->conn);
		mysql_set_charset("utf8",$this->conn);
	}

	public function query($sql){
		return mysql_query($sql);
	}

/**
*	根据pk查一条数据
*/
	public function findByPk($table,$id){
		$sql=sprintf("select * from %s where id ='%s' ",$table,$id);
		$res=$this->query($sql);
		return mysql_fetch_array($res);
	}
/**
*	将resultSet 转换成数组
*/
	public function resToArray($res){

		$tmparr=array();
		while($row=mysql_fetch_array($res)){
			$tmparr[]=$row;		
		}
		return $tmparr;
	}

/**
* 查出所有的数据，只适合小的表
*/
	public function findAll($table){
		$sql=sprintf("select * from %s",$table);
		return resToArray(query($sql));
	}

	public function close(){
		mysql_close($this->conn);
	}


	public function __destory(){
	
		$this->close();

	}





}


?>
